import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;


public class MainWindow {

	public static JFrame mainFrame;
	public static JButton btnSelecionarCor;
	public static DrawPanel drawPanel = new DrawPanel();

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow.init();
					MainWindow.mainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private static void init() {
		mainFrame = new JFrame();
		mainFrame.setTitle("Trabalho 4");
		mainFrame.setBounds(100, 100, 607, 498);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 593};
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		mainFrame.getContentPane().setLayout(gridBagLayout);
		
		JPanel panel_1 = new JPanel();
		mainFrame.getContentPane().add(panel_1);

		JButton btnLoadFile = new JButton("Load");
		btnLoadFile.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent event) {
				String defaultDir = MainWindow.class.getResource("MainWindow.class")
						.getFile();
				JFileChooser fileChooser = new JFileChooser(defaultDir);
				fileChooser.setFileFilter(new FileFilter() {
					@Override
					public String getDescription() {
						return "Text file (.txt)";
					}

					@Override
					public boolean accept(File f) {
						return f.getName().toLowerCase().endsWith(".txt")
								|| f.isDirectory();
					}
				});
				int returnValue = fileChooser.showOpenDialog(null);
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					String filename = fileChooser.getSelectedFile()
							.getAbsolutePath();
					try {
						Controller.getInstance().loadMeshFromFile(
								filename);
					} catch (Exception e) {
						e.printStackTrace();
						JOptionPane.showMessageDialog(mainFrame, e.getMessage());
					}
				}
			}
		});
		panel_1.add(btnLoadFile);
		

		final JCheckBox chckbxRasterizacap = new JCheckBox("Rasterizar");
		chckbxRasterizacap.setSelected(true);
		panel_1.add(chckbxRasterizacap);
				
		final JCheckBox checkWireFrame = new JCheckBox("Wireframe");
		checkWireFrame.setSelected(false);
		panel_1.add(checkWireFrame);
		
		checkWireFrame.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				drawPanel.setWireframeEnable(true);
				if(checkWireFrame.isSelected()){
					drawPanel.setWireframe(Wireframe.getDefaultType());
					
				}else{
					drawPanel.setWireframeEnable(false);
				}
				drawPanel.repaint();
			}
		});
		
		JComboBox cboxCor = new JComboBox();
		panel_1.add(cboxCor);
		cboxCor.setModel(new DefaultComboBoxModel(new String[] {
				"Branco","Rosa", "Vermelho", "Azul", "Verde","Amarelo" }));
		cboxCor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JComboBox comboBox = (JComboBox) arg0.getSource();
				Color branco = new Color(255, 255, 255);
				Color amarelo = new Color(255, 255, 0);
			    Color rosa = new Color(255, 0, 127);
				Color preto = new Color(0, 0, 0, 0);
			    Color vermelho = new Color(255, 0, 0, 255);
			    Color azul = new Color(0, 0, 255, 255);
			    Color verde = new Color(0, 255, 0, 255);
				switch (comboBox.getSelectedIndex()) {
				case 0:
					drawPanel.setColor(branco);
					break;
				case 1:
					drawPanel.setColor(rosa);
					break;
				case 2:
					drawPanel.setColor(vermelho);
					break;
				case 3:
					drawPanel.setColor(azul);
					break;
				case 4:
					drawPanel.setColor(verde);
					break;	
				case 5:
					drawPanel.setColor(amarelo);
					break;	
				}
				drawPanel.repaint();
			}
		});

		
		chckbxRasterizacap.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent event) {
				drawPanel.setRasterizationEnable(chckbxRasterizacap.isSelected());
				drawPanel.repaint();
			}
		});
		
		JButton btnApagar = new JButton("Apagar");
		btnApagar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				drawPanel.getPoints().clear();
				drawPanel.repaint();
			}
		});
		panel_1.add(btnApagar);
		

		drawPanel.setBackground(Color.BLACK);
		drawPanel.setLayout(null);
		GridBagConstraints gbc_canvas = new GridBagConstraints();
		gbc_canvas.fill = GridBagConstraints.BOTH;
		gbc_canvas.gridx = 0;
		gbc_canvas.gridy = 3;
		mainFrame.getContentPane().add(drawPanel, gbc_canvas);
	}

	
}
